/*
  +----------------------------------------------------------------------+
  | PHP Version 7                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2015 The PHP Group                                |
  +----------------------------------------------------------------------+
  | This source file is subject to version 3.01 of the PHP license,      |
  | that is bundled with this package in the file LICENSE, and is        |
  | available through the world-wide-web at the following url:           |
  | http://www.php.net/license/3_01.txt                                  |
  | If you did not receive a copy of the PHP license and are unable to   |
  | obtain it through the world-wide-web, please send a note to          |
  | license@php.net so we can mail you a copy immediately.               |
  +----------------------------------------------------------------------+
  | Author:                                                              |
  +----------------------------------------------------------------------+
*/

/* $Id$ */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#define PHP_COMPILER_ID "VC14"
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_sample.h"

/* If you declare any globals in php_sample.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(sample)
*/
static zend_class_entry *sample_class_entry_ptr;

/* True global resources - no need for thread safety here */
static int le_sample;

/* {{{ PHP_INI
 */
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
    STD_PHP_INI_ENTRY("sample.global_value",      "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_sample_globals, sample_globals)
    STD_PHP_INI_ENTRY("sample.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_sample_globals, sample_globals)
PHP_INI_END()
*/
/* }}} */

/* Remove the following function when you have successfully modified config.m4
   so that your module can be compiled into PHP, it exists only for testing
   purposes. */

/* Every user-visible function in PHP should document itself in the source */
/* {{{ proto string sampleIsInstalled(string arg)
   Return a string to confirm that the module is compiled in */
PHP_FUNCTION(sampleIsInstalled)
{
	RETURN_STRING("Congratulations! Extension SAMPLE is now compiled into PHP.");
}
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and
   unfold functions in source code. See the corresponding marks just before
   function definition, where the functions purpose is also documented. Please
   follow this convention for the convenience of others editing your code.
*/

/* {{{ proto string fnTestingArgs(string a, int b)
   This function has an optional second argument. */
PHP_FUNCTION(fnTestingArgs)
{
	char *a = NULL;
	int argc = ZEND_NUM_ARGS();
	size_t a_len;
	zend_long b = 1234567;

	php_printf("Input Arguments: %ld<br>", argc);
	switch (argc)
	{
	case 1:
		if (zend_parse_parameters(argc TSRMLS_CC, "s", &a, &a_len) == FAILURE)
			return;
		break;
	case 2:
		if (zend_parse_parameters(argc TSRMLS_CC, "sl", &a, &a_len, &b) == FAILURE)
			return;
		break;
	default:
		php_error(E_ERROR, "You should input at least 1 argument and at most 2.");
	}

	php_printf("%s => %llu<br>", a, b);
}
/* }}} */

/* {{{ proto void Sample::__construct(string type)
   This is the constructor of Class Sample. */
PHP_FUNCTION(sample_construct)
{
	zval *this_object = getThis();

	char *sType;
	int uTypeLen;
	double tax = 0.05;
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|d", &sType, &uTypeLen, &tax) == FAILURE) {
		return;
	}
	zend_update_property_string(sample_class_entry_ptr, this_object, "type", 4, sType);
	zend_update_property_double(sample_class_entry_ptr, this_object, "tax", 3, tax);
}
/* }}} */

/* {{{ proto void Sample::getPayment()
   This is a method of Class Sample. */
PHP_FUNCTION(sample_get_payment)
{
	zval *this_object = getThis();
	zval *p;
	zval *t;

	p = zend_read_property(sample_class_entry_ptr, this_object, "payment", 7, 0, NULL);
	t = zend_read_property(sample_class_entry_ptr, this_object, "tax", 3, 0, NULL);
	RETURN_DOUBLE(Z_LVAL_P(p) * (1.0 + Z_DVAL_P(t)));
}
/* }}} */

/* {{{ proto void Sample::getType()
   This is a method of Class Sample. */
PHP_FUNCTION(sample_get_type)
{
	zval *this_object = getThis();
	zval *p;

	p = zend_read_property(sample_class_entry_ptr, this_object, "type", 4, 0, NULL);
	RETURN_STR(Z_STR_P(p));
}
/* }}} */

/* {{{ proto int fnNSTester(float k)
   This function is of namespace \sample\fn */
PHP_FUNCTION(fnNSTester)
{
	int argc = ZEND_NUM_ARGS();
	double k;
	if (zend_parse_parameters(argc TSRMLS_CC, "d", &k) == FAILURE) 
		return;

	php_printf("This function is of namespace \\Sample\\fn.");

	RETURN_LONG((zend_long)(k * 2000));
}
/* }}} */

/* {{{ php_sample_init_globals
 */
/* Uncomment this function if you have INI entries
static void php_sample_init_globals(zend_sample_globals *sample_globals)
{
	sample_globals->global_value = 0;
	sample_globals->global_string = NULL;
}
*/
/* }}} */



/* {{{ sample_functions[]
 *
 * Every user visible function must have an entry in sample_functions[].
 */
const zend_function_entry sample_functions[] = {
	PHP_FE(sampleIsInstalled,	NULL)		/* For testing, remove later. */
	PHP_FE(fnTestingArgs,	NULL)
	ZEND_NS_FE("sample\\fn", fnNSTester, NULL)
	PHP_FE_END	/* Must be the last line in sample_functions[] */
};

const zend_function_entry php_sample_class_functions[] = {
	PHP_FALIAS(Sample,				sample_construct,			NULL)
	PHP_FALIAS(getPayment,			sample_get_payment,			NULL)
	PHP_FALIAS(getType,				sample_get_type,			NULL)
	PHP_FE_END
};

/* }}} */

/* {{{ PHP_MINIT_FUNCTION
 */
PHP_MINIT_FUNCTION(sample)
{

	zend_class_entry sample_class_entry;
	INIT_CLASS_ENTRY(sample_class_entry, "Sample", php_sample_class_functions);
	sample_class_entry_ptr = zend_register_internal_class(&sample_class_entry TSRMLS_CC);

	/* Defined public member of Class Sample. */

	zend_declare_property_long(sample_class_entry_ptr, "payment", 7, 0, ZEND_ACC_PUBLIC);

	zend_declare_property_null(sample_class_entry_ptr, "tax", 3, ZEND_ACC_PUBLIC);

	zend_declare_property_null(sample_class_entry_ptr, "type", 4, ZEND_ACC_PROTECTED);

	/* Defined constants of Class Sample. */

	zend_declare_class_constant_string(sample_class_entry_ptr, "TYPE_DOLLAR", 11, "dollar");
	zend_declare_class_constant_string(sample_class_entry_ptr, "TYPE_RMB", 8, "rmb");

	// Defined normal variable.

	REGISTER_LONG_CONSTANT("SAMPLE_TEST_LONG", 123456, CONST_CS | CONST_PERSISTENT);
	REGISTER_BOOL_CONSTANT("SAMPLE_TEST_BOOL", 1, CONST_CS | CONST_PERSISTENT);

	// News in PHP7: supporting 64-bits integer.

	REGISTER_LONG_CONSTANT("SAMPLE_TEST_LONG64", 666666666666666, CONST_CS | CONST_PERSISTENT);

	// Defined string in 2 ways.

	REGISTER_STRING_CONSTANT("SAMPLE_TEST_STRING", "hello", CONST_PERSISTENT);
	REGISTER_STRINGL_CONSTANT("SAMPLE_TEST_STRING2", "hello", 5, CONST_CS | CONST_PERSISTENT);

	// Defined constants in namespaces.

	REGISTER_NS_BOOL_CONSTANT("sample\\consts", "TEST_BOOL_VALUE", 1, CONST_CS | CONST_PERSISTENT);
	REGISTER_NS_LONG_CONSTANT("sample\\consts", "TEST_LONG_VALUE", 2333333, CONST_CS | CONST_PERSISTENT);

	REGISTER_NS_STRING_CONSTANT("sample\\consts", "TEST_STRING_VALUE", "hello", CONST_CS | CONST_PERSISTENT);

	REGISTER_NS_STRINGL_CONSTANT("sample\\consts", "TEST_STRING2_VALUE", "hello", 5, CONST_CS | CONST_PERSISTENT);

	return SUCCESS;
}
/* }}} */

/* {{{ PHP_MSHUTDOWN_FUNCTION
 */
PHP_MSHUTDOWN_FUNCTION(sample)
{
	/* uncomment this line if you have INI entries
	UNREGISTER_INI_ENTRIES();
	*/
	return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
 */
PHP_RINIT_FUNCTION(sample)
{
#if defined(COMPILE_DL_SAMPLE) && defined(ZTS)
	ZEND_TSRMLS_CACHE_UPDATE();
#endif
	return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
 */
PHP_RSHUTDOWN_FUNCTION(sample)
{
	return SUCCESS;
}
/* }}} */

/* {{{ PHP_MINFO_FUNCTION
 */
PHP_MINFO_FUNCTION(sample)
{
	php_info_print_table_start();
	php_info_print_table_header(2, "sample support", "enabled");
	php_info_print_table_end();

	/* Remove comments if you have entries in php.ini
	DISPLAY_INI_ENTRIES();
	*/
}
/* }}} */

/* {{{ sample_module_entry
 */
zend_module_entry sample_module_entry = {
	STANDARD_MODULE_HEADER,
	"PHP 7.0 Extension Sample",
	sample_functions,
	PHP_MINIT(sample),
	PHP_MSHUTDOWN(sample),
	PHP_RINIT(sample),		/* Replace with NULL if there's nothing to do at request start */
	PHP_RSHUTDOWN(sample),	/* Replace with NULL if there's nothing to do at request end */
	PHP_MINFO(sample),
	PHP_SAMPLE_VERSION,
	STANDARD_MODULE_PROPERTIES
};
/* }}} */

#ifdef COMPILE_DL_SAMPLE
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE();
#endif
ZEND_GET_MODULE(sample)
#endif

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */
